<?php
namespace backend\models\friend;
use yii\db\ActiveRecord;

class User extends ActiveRecord
{

	/**
	 * @return string 返回该AR类关联的数据表名
	*/

	public static function tableName()
	{
		return '{{%user}}';
	}

	/**
	*	搜索
	*/
	public function searchUser($nickname){
		$re= User::find()->where(['or',['user_num'=>$nickname],['user_name'=>$nickname]])->asarray()->one();
		if(!$re) return 1;   //用户不存在
		$user_id = $re['user_id'];
		$cond = ['user_id'=>$user_id];
		$res = Friend::find()->where($cond)->asarray()->all();
		if(count($res)==0) return 2; //此用户暂无好友
		for($i=0;$i<count($res);$i++){
			$data[] = $res[$i]['to_id'];
		}
		$cond = ['in', 'user_id', $data];
		$arr = array('re'=>$re,'cond'=>$cond);
		return $arr;
	}

	/**
	*	下载查询
	*/
	public function downloadSel($nickname){
		$re= User::find()
            ->where(['or',['user_num'=>$nickname],['user_name'=>$nickname]])
            ->asarray()->one();
		if(empty($re)) return false;
		$user_id = $re['user_id'];
		$cond = ['user_id'=>$user_id];
		$res = Friend::find()->where($cond)->asarray()->all();
		for($i=0;$i<count($res);$i++){
			$data[] = $res[$i]['to_id'];
		}
		
		$cond = ['in', 'user_id', $data];
		
        $data = self::find()
            ->select(['user_num','user_name','user_regtime'])
            ->where($cond)->asArray()->all();
        
		foreach($data as $k=>$v){
			$data[$k]['user_regtime'] = date('Y-m-d H:i:s',$v['user_regtime']);
		}
		return $data;
	}
}